package com.yanqu.road.server.cmd.union.war;

import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.entity.union.UnionInfo;
import com.yanqu.road.entity.union.UnionUpgradeInfo;
import com.yanqu.road.pb.unionwar.UnionWarProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.config.UnionConfigMgr;
import com.yanqu.road.server.manger.union.UnionMgr;
import com.yanqu.road.server.manger.union.UnionWarMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.utils.string.StringUtils;

@Cmd(code = Protocol.S_UNION_WAR_RECALL_PATRONS, desc = "商会战召回门客")
public class RecallPatronsCmd extends AbstractCommand {

    private int patronsId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        UnionWarProto.RecallPatronsReqMsg reqMsg = UnionWarProto.RecallPatronsReqMsg.parseFrom(packet.getBytes());
        patronsId = reqMsg.getPatronsId();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        String unionUid = player.getUserInfo().getUnionUid();
        if (StringUtils.isNullOrEmpty(unionUid)) {
            ret = GameErrorCode.E_UNION_NO_JOIN_UNION;
            return;
        }
        UnionInfo unionInfo = UnionMgr.getUnionInfo(unionUid);
        if (null == unionInfo) {
            ret = GameErrorCode.E_UNION_NO_JOIN_UNION;
            return;
        }
        UnionUpgradeInfo unionUpgradeInfo = UnionConfigMgr.getUnionUpgradeInfo(unionInfo.getExperience());
        int unionLv = 0;
        if(null != unionUpgradeInfo){
            unionLv = unionUpgradeInfo.getLevel();
        }
        if (unionLv < GameConfig.UNION_WAR_MIN_LEVEL) {
            ret = GameErrorCode.E_UNION_LEVEL_NO_ENOUGH;
            return;
        }

        ret = UnionWarMgr.recallPatrons(player, patronsId);
    }

    @Override
    public void sendData(GamePlayer player) {
        UnionWarProto.RecallPatronsRespMsg.Builder respMsg = UnionWarProto.RecallPatronsRespMsg.newBuilder();
        respMsg.setRet(ret);
        player.sendPacket(Protocol.U_UNION_WAR_RECALL_PATRONS, respMsg);
    }
}
